package com.example.concurrentprogramming8016.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author liyanhao
 * @Date 2023-01-07 18:19
 */
public class ArrayBlockingQueueTest {
    private static ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(2);

    public static void main(String[] args) throws InterruptedException {
        //超过最大长度直接抛出异常
        //addTest();
        //删除一个不存在的元素会返回false
//        removeTest();
        // queue为空时候直接抛出异常 不会为空则返回队首的元素
//        arrayBlockingQueue.add(1);
//        elementTest();
        //offer添加元素如果满了不会阻塞或者报错 返回false
//        offerTest();
        // 弹出队首的元素 如果没有则直接返回null 不会阻塞
//        pollTest();
        // 如果队列为空则直接返回null 返回队头的元素 但是不会删除元素
//        peekTest();
        //队列满了调用 put方法会阻塞
//        putTest();

        // 如果队列里边的元素被take空了 take方法就会阻塞起来
        arrayBlockingQueue.put(1);
        arrayBlockingQueue.put(2);
        takeTest();



    }

    private static void takeTest(){
        try {
            Object take = arrayBlockingQueue.take();
            System.out.println(take);
            Object take1 = arrayBlockingQueue.take();
            System.out.println(take1);
            Object take2 = arrayBlockingQueue.take();
            System.out.println(take2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    private static void putTest(){
        try {
            arrayBlockingQueue.put(1);
            System.out.println("添加成功1");
            arrayBlockingQueue.put(2);
            System.out.println("添加成功2");
            arrayBlockingQueue.put(3);
            System.out.println("添加成功3");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
    private static void peekTest(){
        System.out.println(arrayBlockingQueue.peek());

    }

    private static void pollTest(){
        arrayBlockingQueue.add(1);
        arrayBlockingQueue.add(2);
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
    }

    private static void offerTest(){
        System.out.println(arrayBlockingQueue.offer(1));
        System.out.println(arrayBlockingQueue.offer(2));
        System.out.println(arrayBlockingQueue.offer(3));
    }
    public static void elementTest(){
        Object element = arrayBlockingQueue.element();
        System.out.println(element);
    }

    public static void removeTest(){
        System.out.println(arrayBlockingQueue.add(1));
        System.out.println(arrayBlockingQueue.add(2));
        System.out.println(arrayBlockingQueue.remove(1));
        System.out.println(arrayBlockingQueue.remove(2));
        System.out.println(arrayBlockingQueue.remove(3));

    }

    private static void addTest() {
        System.out.println(arrayBlockingQueue.add(1));
        System.out.println(arrayBlockingQueue.add(2));
        System.out.println(arrayBlockingQueue.add(3));
    }
}
